﻿
(function () {
    var forgot = {
        init: function () {
            var old = false;//旧密码
            var newp = false;//新密码
            var ispwd = false;//两次输入
            var isMobile=false; //手机号
            this.bindEvents();
        },
        bindEvents: function () {
            var self = this;
            //旧密码验证start
            $("#old-pwd").blur(function () {
                self.isPwdNull();
            });
            //新密码验证start
            $("#new-pwd").blur(function () {
                self.isnewPwdNull();
            });
            //两次输入密码是否一致验证
            $("#repeat-new-pwd").blur(function () {
               self.Pwd();
            });
            //确定按钮
            $("#sure_btn").click(function () {
                self.submitPwd();
            });
            //关闭错误提示按钮
            $(".colse-tip-btn").click(function () {
                $(".pwd-tip-box").hide();
               // 
            });

            //获取手机验证码start
            $("#code_btn").on('click',function(){
               self.submitCode();
            });
            //获取手机验证码end


            //检测手机号是否已被注册
            $("#mobile").blur(function(){
               mobileIsExit();
            })

        },
        //旧密码验证
        isPwdNull: function () {
            if ($("#old-pwd").val() == "") {
                $(".old-error").html("<font color='red'>*</font>");
                old = false;
            }
            else {
                old = true;
                $(".old-error").html("<font color='red'></font>");
            }
        },
        //新密码验证
        // isnewPwdNull: function () {
        //     if ($("#new-pwd").val() == "") {
        //         $(".new-error").html("<font color='red'>*</font>");
        //         newp = false;
        //     }
        //     else {
        //         newp = true;
        //         $(".new-error").html("<font color='red'></font>");
        //     }
        // },

        isnewPwdNull:function (){
            var regPwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
            var pwdVal=$("#new-pwd").val();
            if(!regPwd.test(pwdVal)){
                myApp.alert("密码由6-16位字母、数字组成")
                ispwd =false;
            }else{
                ispwd = true;
            }
        },


        //两次输入密码是否一致验证
        Pwd: function () {
            if ($("#repeat-new-pwd").val() == "") {
                $(".repeat-error").html("<font color='red'>*</font>");
                ispwd = false;
            } else if ($("#repeat-new-pwd").val() != $("#new-pwd").val()) {
                $(".repeat-error").html("<font color='red'>"+_("不一致")+"</font>");
                ispwd = false;
            }
            else {
                ispwd = true;
                $(".repeat-error").html("<font color='red'></font>");
            }
        },



        //获取验证码
        submitCode:function(){
            var submitJson={};
            var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/
            var mobileVal=$("#mobile").val();
            if(mobile.test(mobileVal)){
                submitJson.Tel=mobileVal;
                $("#code_btn").attr("disabled","disabled");
                //调用上面自定义的函数
                $.setTimeA(60);
            }else{
                myApp.alert("请输入正确的手机号");
                return false;
            }

            $.ajax({
                type:"POST",
                url: "/Customer/ValidateCode",
                data: JSON.stringify(submitJson),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    var res=JSON.parse(result);
                    if(res.msgcode==false){
                        myApp.alert(res.msginfo);
                        window.clearTimeout(getCodeTime);
                        $("#code_btn").attr("disabled",false);//按钮可用
                        $("#code_btn").val("获取验证码");
                    }
                }
            });
        },
        //确定
        submitPwd: function () {
            var self = this;
            var oldpwd = $("#old-pwd").val();
            var newpwd = $("#new-pwd").val();
            var repeatnewpwd = $("#repeat-new-pwd").val();
            var mobile=$("#mobile").val();
            var smscode=$("#_smsCode").val();
            if (oldpwd=="" || newpwd =="" || repeatnewpwd=="" || newpwd!=repeatnewpwd) {
               	myApp.alert(_("请输入密码！"));
            }else if(mobile=="" || smscode==""){
                myApp.alert(_("请输入手机号或验证码！"));
            }else {
            	 self.customerModifyPassword();
               
            }
        },
        //修改密码
        customerModifyPassword:function(){
            var requestCustomer = {};
            requestCustomer.CustomerID = CustomerID;
            requestCustomer.OldUserPassword = $("#old-pwd").val();
            requestCustomer.NewUserPassword = $("#new-pwd").val();
            requestCustomer.Mobile=$('#mobile').val();
            requestCustomer.SmsCode=$("#_smsCode").val();
            //console.log(requestCustomer)
            $.ajax({
                url: "/Customer/CustomerModifyPassword",//提交地址
                type: "POST",//提交方式
                data: JSON.stringify(requestCustomer),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    var _result = eval('(' + result + ')');//eval()解析单个json对象
                    debugger
                    if (_result["msgcode"] == true) {
                    	 myApp.alert(_('修改成功,请重新登录！'), function () {
					        if(is_app()){
	                        	var reset_json={};
	                        	reset_json.result=true;
	                      		reset_json.userId=CustomerID;
		                      	reset_json.pwd=$("#new-pwd").val();
	                        	reset_json.type="1";
	                        	YiYi_APP.resetPwd(JSON.stringify(reset_json));
	                    	}else {
	                        	window.location.href = "/customer/index";
	                      	}
						 });

                    } else {
                        myApp.alert(_result["msginfo"]);
                         if(is_app()){
                        	var reset_json={};
                        	reset_json.result=false;
                        	reset_json.msg=_result["msginfo"];
                        	YiYi_APP.resetPwd(JSON.stringify(reset_json));
                        }
                    }
                }
            });
        },
    };
    forgot.init();
})();

//JQ自定义一个函数  倒计时
var getCodeTime;
$.extend({
    "setTimeA":function(b) {
        if(parseInt(b)>1) {
            var timeStart = parseInt(b) - 1;
            $("#code_btn").val(timeStart+"s");//显示秒数
            getCodeTime=setTimeout(function(){
                $.setTimeA(timeStart);
            },1000);
        }else{
            $("#code_btn").attr("disabled",false);//按钮可用
            $("#code_btn").val("获取验证码");
        }

    }
});


//检测手机号是否已被注册
function mobileIsExit(){
    if ($("#mobile").val() == "") {//为空的话
        $(".mobile_error").html("<font color='red'>*</font>");
        isMobile = false;
    } else {
        $.ajax({
            url: "/Customer/ValidateCustomerMobileIsExist",//提交地址
            type: "POST",//提交方式
            data: "{'Mobile':'" + $("#mobile").val() + "','CustomerID':'" + CustomerID + "'}",
            async: false,
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                var _result = eval('(' + result + ')');//eval()解析单个json对象
                if (_result['msgcode'] == false) {//已被注册
                    $(".mobile_error").html("<font color='red'>"+_("已注册")+"</font>");
                    isMobile = false;//账号的变量为false
                } else {//未被注册
                    $(".mobile_error").html("<font color='red'></font>");
                    isMobile = true;
                }
            },
            error: function (err) {
                alert("err:" + err);
            }
        });
    }
}





